﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using uMES.LeanManufacturing.Common;
using uMES.LeanManufacturing.ParameterDTO;
using uMES.LeanManufacturing.ReportBusiness;

public partial class BwDataCollectReportForm : PageBase
{
    int m_PageSize = 50;
    string webRootUrl = "~";
    string webRootDir = HttpRuntime.AppDomainAppPath;
    uMESReportBusiness reportBal = new uMESReportBusiness();
    private uMESCommonBusiness common = new uMESCommonBusiness();

    protected void Page_Load(object sender, EventArgs e)
    {
        uMESMasterPage master = this.Master as uMESMasterPage;
        master.strNavigation = "当前位置：关重件数据采集报表";
        master.strTitle = "关重件数据采集报表";
        master.ChangeFrame(true);
        upageTurning.PageIndexChanged += new pageTurning.PageIndexChangedEventHandler(() => { SearchData(upageTurning.CurrentPageIndex.ToString()); });
        //WebPanel = WebAsyncRefreshPanel1;
        ClearMessage();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {

        try
        {
            gdResult.Rows.Clear();
            SearchData("1");
        }
        catch (Exception ex)
        {
            DisplayMessage(ex.Message, false);
        }
    }
    protected void btnReSet_Click(object sender, EventArgs e)
    {
        txtContainerName.Text = "";
        txtProcessNo.Text = "";
        txtSpecName.Text = "";
        txtProduct.Text = "";

        gdResult.Rows.Clear();
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        try
        {
            ExportToExcel();
        }
        catch (Exception ex)
        {
            DisplayMessage(ex.Message, false);
        }
    }
    void SearchData(string index)
    {

        uMESPagingDataDTO result = reportBal.GetDataCollectionInfo(GetQuery(index));

        for (int i = 0; i < result.DBTable.Rows.Count; i++)
        {
            string strCheckitem = result.DBTable.Rows[i]["checkitem"].ToString();
            string strCheckitemDis = string.Empty;
            //ParseCode(strCheckitem, ref dtNew, ref strCheckitemDis);
            ParseCode(strCheckitem, ref strCheckitemDis);
            result.DBTable.Rows[i]["checkitem"] = strCheckitemDis.Replace("㊣", "");

            strCheckitemDis = string.Empty;
            ParseCode(result.DBTable.Rows[i]["checkvalue"].ToString(), ref strCheckitemDis);
            result.DBTable.Rows[i]["checkvalue"] = strCheckitemDis.Replace("㊣", "");

            strCheckitemDis = string.Empty;
            ParseCode(result.DBTable.Rows[i]["checkvalue1"].ToString(), ref strCheckitemDis);
            result.DBTable.Rows[i]["checkvalue1"] = strCheckitemDis.Replace("㊣", "");
        }
        this.gdResult.DataSource = result.DBTable;
        this.gdResult.DataBind();
        //给分页控件赋值，用于分页控件信息显示
        this.upageTurning.TotalRowCount = int.Parse(result.RowCount);
        this.upageTurning.RowCountByPage = m_PageSize;

    }

    protected Dictionary<string, string> GetQuery(string strIndex)
        {
        Dictionary<string, string> para = new Dictionary<string, string>();
        para["CurrentPageIndex"] = strIndex.ToString();
        para["PageSize"] = m_PageSize.ToString();

        para["ProcessNo"] = txtProcessNo.Text.Trim();
        para["ContainerName"] = txtContainerName.Text.Trim();
        para["Product"] = txtProduct.Text.Trim();
        para["StepName"] = txtSpecName.Text.Trim();
        //para["StartDate"] = txtStartDate.Value.Trim();
        //para["EndDate"] = txtEndDate.Value.Trim();
        return para;

    }

    void ExportToExcel() {

        // DataTable dt = uMESCommonFunction.GetGridData(gdResult);
        uMESPagingDataDTO result = reportBal.GetDataCollectionInfo(GetQuery("all"));

        for (int i = 0; i < result.DBTable.Rows.Count; i++)
        {
            string strCheckitem = result.DBTable.Rows[i]["checkitem"].ToString();
            string strCheckitemDis = string.Empty;
            //ParseCode(strCheckitem, ref dtNew, ref strCheckitemDis);
            ParseCode1(strCheckitem, ref strCheckitemDis);
            result.DBTable.Rows[i]["checkitem"] = MerageImagsAndStrs(strCheckitemDis, 0);

            strCheckitemDis = string.Empty;
            ParseCode1(result.DBTable.Rows[i]["checkvalue"].ToString(), ref strCheckitemDis);
            result.DBTable.Rows[i]["checkvalue"] = MerageImagsAndStrs(strCheckitemDis, 0);

            strCheckitemDis = string.Empty;
            ParseCode1(result.DBTable.Rows[i]["checkvalue1"].ToString(), ref strCheckitemDis);
            result.DBTable.Rows[i]["checkvalue1"] = MerageImagsAndStrs(strCheckitemDis, 0) ;
        }
        DataSet ds = new DataSet();

        DataView view = result.DBTable.DefaultView;

        result.DBTable = view.ToTable(false, "ProcessNo", "ProductName", "ProductDesc", "ContainerName", "conqty", "SpecNameDisp", "checkiteminfoname"
            , "checkitem", "checktype", "checkvalue","checkvalue1");

        result.DBTable.Columns["ProcessNo"].ColumnName = "工作令号";
        result.DBTable.Columns["ProductName"].ColumnName = "图号";
        result.DBTable.Columns["ProductDesc"].ColumnName = "名称";
        result.DBTable.Columns["ContainerName"].ColumnName = "批次号";
        result.DBTable.Columns["ConQty"].ColumnName = "批次数量";
        result.DBTable.Columns["SpecNameDisp"].ColumnName = "工序名称";
        result.DBTable.Columns["checkiteminfoname"].ColumnName = "检测项名称";

        result.DBTable.Columns["checkitem"].ColumnName = "检测项内容";

        result.DBTable.Columns["checktype"].ColumnName = "检查类型";

        result.DBTable.Columns["checkvalue"].ColumnName = "实测值(工人)";
        result.DBTable.Columns["checkvalue1"].ColumnName = "实测值(检验)";


        //NOPI方式导出
        DataTableToExcel excel1 = new DataTableToExcel();
        try
        {
            result.DBTable.TableName = "关重件数据采集报表";
            ds.Tables.Add(result.DBTable);
            excel1.RenderToExcel(ds, Context, System.DateTime.Now.ToString("yyyyMMddhhss") + ".xls");
        }
        catch (Exception ex)
        {
            DisplayMessage(ex.Message, false);
        }
    }


    public void ParseCode(String strPreviewCode, ref string strPreviewCodeDis)
    {
        try
        {
            String strPreviewCodeTemp, strHtmlStripped, strTemp, strCodeTemp;
            String strFileDoc, strFileTemp, strFilePath, strFileName, strMapPath, strHtml;
            int intStartFlag, intEndFlag, intFlag1, intFlag2, intCodeStartFlag, intCodeEndFlag;
            String strImageIndex;
            clsParseCode oParse = new clsParseCode();
            Bitmap objBmp;
            String strLeft, strUp, strDown, strRight;

            strPreviewCode = strPreviewCode.Trim().Replace("\r\n\t\t", "");
            strPreviewCode = strPreviewCode.Trim().Replace("<P>", "");
            strPreviewCode = strPreviewCode.Trim().Replace("</P>", "");
            strPreviewCode = strPreviewCode.Replace("\"", "'");

            intStartFlag = strPreviewCode.IndexOf("<Image>");
            intEndFlag = strPreviewCode.IndexOf("</Image>");
            strHtmlStripped = "";
            strHtml = "";

            //strMapPath = MapPath("~");
            //strFileTemp = strMapPath + @"\Images\";

            //clsCon oCon = new clsCon();
            String strImagePath;
            //strImagePath = oCon.LoadConfigString("ImageGetPath");

            strImagePath = webRootDir + ConfigurationManager.AppSettings["ImageGetPath"].ToString();

            strFileTemp = strImagePath;

            uMESExternalControl.ToleranceInputLib.clsDrawImage oDraw = new uMESExternalControl.ToleranceInputLib.clsDrawImage();
            DataTable dtImage = new DataTable();

            while (intStartFlag > -1)
            {
                if (intStartFlag > 0)
                {
                    //将纯文本输出
                    strHtml = strPreviewCode.Substring(0, intStartFlag);
                    strPreviewCode = strPreviewCode.Substring(intStartFlag);
                    strPreviewCodeDis += strHtml + "㊣"; ;
                    intStartFlag = strPreviewCode.IndexOf("<Image>");
                    intEndFlag = strPreviewCode.IndexOf("</Image>");
                    continue;
                }

                else
                {
                    strHtml = "";
                    strPreviewCodeTemp = strPreviewCode.Substring(intStartFlag + 7, intEndFlag - intStartFlag - 7);
                    intCodeStartFlag = strPreviewCodeTemp.IndexOf("<&70><+>");
                    intCodeEndFlag = strPreviewCodeTemp.IndexOf("<+><&90>");

                    if (intCodeStartFlag > -1)
                    {
                        //代码为行位公差时
                        strCodeTemp = strPreviewCodeTemp.Replace("<&70><+>", "");
                        strCodeTemp = strCodeTemp.Replace("<+><&90>", "");

                        intFlag1 = strCodeTemp.IndexOf("<");
                        intFlag2 = strCodeTemp.IndexOf(">");
                        while (intFlag1 > -1)
                        {
                            strTemp = strCodeTemp.Substring(intFlag1, intFlag2 - intFlag1 + 1);
                            strCodeTemp = strCodeTemp.Replace(strTemp, "");
                            intFlag1 = strCodeTemp.IndexOf("<");
                            intFlag2 = strCodeTemp.IndexOf(">");
                        }

                        strHtmlStripped = strCodeTemp;
                        //strPreviewCodeTemp = strPreviewCode;
                        strPreviewCodeTemp = strPreviewCodeTemp.Replace(@"<Image>", "");
                        strPreviewCodeTemp = strPreviewCodeTemp.Replace(@"</Image>", "");

                        objBmp = oDraw.DrawShapeTolerance(strPreviewCodeTemp, strHtmlStripped, strFileTemp);
                    }
                    else
                    {
                        intCodeStartFlag = strPreviewCodeTemp.IndexOf("<T");
                        intCodeEndFlag = strPreviewCodeTemp.IndexOf("!");
                        if (intCodeStartFlag > -1)
                        {
                            //代码为上下标公差

                            strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag);
                            strUp = strPreviewCodeTemp.Substring(intCodeStartFlag + 2, intCodeEndFlag - intCodeStartFlag - 2);
                            strDown = strPreviewCodeTemp.Substring(intCodeEndFlag + 1, strPreviewCodeTemp.Length - intCodeEndFlag - 2);
                            objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown);
                        }
                        else
                        {
                            intCodeStartFlag = strPreviewCodeTemp.IndexOf("<H>");
                            intCodeEndFlag = strPreviewCodeTemp.IndexOf("</H>");
                            if (intCodeStartFlag > -1)
                            {
                                //代码为只有上公差
                                strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag);
                                strUp = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3);
                                strDown = "";
                                objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown);
                            }
                            else
                            {
                                intCodeStartFlag = strPreviewCodeTemp.IndexOf("<L>");
                                intCodeEndFlag = strPreviewCodeTemp.IndexOf("</L>");
                                if (intCodeStartFlag > -1)
                                {
                                    //代码为只有下公差
                                    strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag);
                                    strUp = "";
                                    strDown = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3);
                                    objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown);
                                }
                                else
                                {
                                    intCodeStartFlag = strPreviewCodeTemp.IndexOf("<√>");
                                    intCodeEndFlag = strPreviewCodeTemp.IndexOf("</√>");
                                    if (intCodeStartFlag > -1)
                                    {
                                        //代码为指定加工方法时
                                        strLeft = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3);

                                        strRight = strPreviewCodeTemp.Substring(intCodeEndFlag + 4);
                                        objBmp = oDraw.DrawRoughness(strLeft, strRight);
                                    }
                                    else
                                    {
                                        intCodeStartFlag = strPreviewCodeTemp.IndexOf("<R>");
                                        intCodeEndFlag = strPreviewCodeTemp.IndexOf("</R>");
                                        if (intCodeStartFlag > -1)
                                        {
                                            //代码为去除材料时
                                            strLeft = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3);

                                            strRight = "";
                                            objBmp = oDraw.DrawRoughness(strLeft, strRight);
                                        }
                                        else
                                        {
                                            intCodeStartFlag = strPreviewCodeTemp.IndexOf("<Q>");
                                            intCodeEndFlag = strPreviewCodeTemp.IndexOf("</Q>");
                                            if (intCodeStartFlag > -1)
                                            {
                                                //代码为不去除材料时
                                                strLeft = "";

                                                strRight = "";
                                                objBmp = oDraw.DrawRoughness(strLeft, strRight);
                                            }
                                            else
                                            {
                                                objBmp = null;
                                            }
                                        }
                                    }
                                }
                            }

                        }
                    }

                    //保存
                    if (objBmp != null)
                    {
                        //strFileDoc = strMapPath + @"\ImageTemp\";
                        //clsCon oCon = new clsCon();
                        String strImageTempPath;
                        //strImageTempPath = oCon.LoadConfigString("ImageTempPath");
                        strImageTempPath = webRootDir + ConfigurationManager.AppSettings["ImageTempPath"].ToString();

                        strFileDoc = strImageTempPath;
                        //if (Session["intMaxImageIndex"] == null)
                        //{
                        //    Session["intMaxImageIndex"] = 0;
                        //    intImageIndex = 1;
                        //}
                        //else
                        //{
                        //    intImageIndex = (int)Session["intMaxImageIndex"] + 1;
                        //}

                        strImageIndex = System.DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff");
                        strFileName = "ImageTemp-" + strImageIndex + ".png";
                        //strFileName = "ImageTemp-" + intImageIndex + ".png";
                        strFilePath = strFileDoc + strFileName;
                        //System.Threading.Thread.Sleep(1);
                        //System.IO.FileInfo oFile = new System.IO.FileInfo(strFilePath);
                        //while (oFile.Exists == true)
                        //{
                        //    intImageIndex += 1;
                        //    strFileName = "ImageTemp-" + intImageIndex + ".png";
                        //    strFilePath = strFileDoc + strFileName;
                        //    oFile = new System.IO.FileInfo(strFilePath);
                        //}

                        System.IO.FileInfo oFile = new System.IO.FileInfo(strFilePath);
                        while (oFile.Exists == true)
                        {
                            strImageIndex = strImageIndex + "1";
                            strFileName = "ImageTemp-" + strImageIndex + ".png";
                            strFilePath = strFileDoc + strFileName;
                            oFile = new System.IO.FileInfo(strFilePath);
                        }

                        objBmp.Save(strFilePath);
                        // Session["intMaxImageIndex"] = intImageIndex;
                        if (Session["dtImage"] == null)
                        {
                            dtImage = new DataTable();
                            dtImage.Columns.Add("FileName");
                            dtImage.Columns.Add("HtmlCode");
                            Session["dtImage"] = dtImage;
                        }
                        else
                        {
                            dtImage = (DataTable)Session["dtImage"];
                        }

                        DataRow dr;
                        dr = dtImage.NewRow();
                        dr[0] = strFileName;
                        dr[1] = "<Image>" + strPreviewCodeTemp + "</Image>";
                        dtImage.Rows.Add(dr);
                        Session["dtImage"] = dtImage;

                        //strFileDoc = oCon.LoadConfigString("ImageTempPath");
                        //strFileDoc = ConfigurationManager.AppSettings["ImageTempPath"].ToString();
                        strFileDoc = ResolveUrl(webRootUrl + ConfigurationManager.AppSettings["ImageTempPath"].ToString());

                        // strHtml = "<img src='../../../ImageTemp/" + strFileName + "'>";
                        strHtml = @"<img src='" + strFileDoc + strFileName + "'>";
                        strPreviewCodeDis += strHtml + "㊣";
                    }

                }
                if (intEndFlag + 8 < strPreviewCode.Length)
                {
                    strPreviewCode = strPreviewCode.Substring(intEndFlag + 8);
                    intStartFlag = strPreviewCode.IndexOf("<Image>");
                    intEndFlag = strPreviewCode.IndexOf("</Image>");
                }
                else
                {
                    intStartFlag = -1;
                }

            }
            intStartFlag = strPreviewCode.IndexOf("<Image>");
            if (strPreviewCode != "" && intStartFlag == -1)
            {
                strPreviewCodeDis += strPreviewCode;
            }
        }
        catch (Exception myError)
        {

        }

    }


    public void ParseCode1(String strPreviewCode, ref string strPreviewCodeDis)
    {
        try
        {
            String strPreviewCodeTemp, strHtmlStripped, strTemp, strCodeTemp;
            String strFileDoc, strFileTemp, strFilePath, strFileName, strMapPath, strHtml;
            int intStartFlag, intEndFlag, intFlag1, intFlag2, intCodeStartFlag, intCodeEndFlag;
            String strImageIndex;
            clsParseCode oParse = new clsParseCode();
            Bitmap objBmp;
            String strLeft, strUp, strDown, strRight;

            strPreviewCode = strPreviewCode.Trim().Replace("\r\n\t\t", "");
            strPreviewCode = strPreviewCode.Trim().Replace("<P>", "");
            strPreviewCode = strPreviewCode.Trim().Replace("</P>", "");
            strPreviewCode = strPreviewCode.Replace("\"", "'");

            intStartFlag = strPreviewCode.IndexOf("<Image>");
            intEndFlag = strPreviewCode.IndexOf("</Image>");
            strHtmlStripped = "";
            strHtml = "";

            //strMapPath = MapPath("~");
            //strFileTemp = strMapPath + @"\Images\";

            //clsCon oCon = new clsCon();
            String strImagePath;
            //strImagePath = oCon.LoadConfigString("ImageGetPath");

            strImagePath = webRootDir + ConfigurationManager.AppSettings["ImageGetPath"].ToString();

            strFileTemp = strImagePath;

            uMESExternalControl.ToleranceInputLib.clsDrawImage oDraw = new uMESExternalControl.ToleranceInputLib.clsDrawImage();
            DataTable dtImage = new DataTable();

            while (intStartFlag > -1)
            {
                if (intStartFlag > 0)
                {
                    //将纯文本输出
                    strHtml = strPreviewCode.Substring(0, intStartFlag);
                    strPreviewCode = strPreviewCode.Substring(intStartFlag);
                    strPreviewCodeDis += strHtml + "㊣"; ;
                    intStartFlag = strPreviewCode.IndexOf("<Image>");
                    intEndFlag = strPreviewCode.IndexOf("</Image>");
                    continue;
                }

                else
                {
                    strHtml = "";
                    strPreviewCodeTemp = strPreviewCode.Substring(intStartFlag + 7, intEndFlag - intStartFlag - 7);
                    intCodeStartFlag = strPreviewCodeTemp.IndexOf("<&70><+>");
                    intCodeEndFlag = strPreviewCodeTemp.IndexOf("<+><&90>");

                    if (intCodeStartFlag > -1)
                    {
                        //代码为行位公差时
                        strCodeTemp = strPreviewCodeTemp.Replace("<&70><+>", "");
                        strCodeTemp = strCodeTemp.Replace("<+><&90>", "");

                        intFlag1 = strCodeTemp.IndexOf("<");
                        intFlag2 = strCodeTemp.IndexOf(">");
                        while (intFlag1 > -1)
                        {
                            strTemp = strCodeTemp.Substring(intFlag1, intFlag2 - intFlag1 + 1);
                            strCodeTemp = strCodeTemp.Replace(strTemp, "");
                            intFlag1 = strCodeTemp.IndexOf("<");
                            intFlag2 = strCodeTemp.IndexOf(">");
                        }

                        strHtmlStripped = strCodeTemp;
                        //strPreviewCodeTemp = strPreviewCode;
                        strPreviewCodeTemp = strPreviewCodeTemp.Replace(@"<Image>", "");
                        strPreviewCodeTemp = strPreviewCodeTemp.Replace(@"</Image>", "");

                        objBmp = oDraw.DrawShapeTolerance(strPreviewCodeTemp, strHtmlStripped, strFileTemp);
                    }
                    else
                    {
                        intCodeStartFlag = strPreviewCodeTemp.IndexOf("<T");
                        intCodeEndFlag = strPreviewCodeTemp.IndexOf("!");
                        if (intCodeStartFlag > -1)
                        {
                            //代码为上下标公差

                            strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag);
                            strUp = strPreviewCodeTemp.Substring(intCodeStartFlag + 2, intCodeEndFlag - intCodeStartFlag - 2);
                            strDown = strPreviewCodeTemp.Substring(intCodeEndFlag + 1, strPreviewCodeTemp.Length - intCodeEndFlag - 2);
                            objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown);
                        }
                        else
                        {
                            intCodeStartFlag = strPreviewCodeTemp.IndexOf("<H>");
                            intCodeEndFlag = strPreviewCodeTemp.IndexOf("</H>");
                            if (intCodeStartFlag > -1)
                            {
                                //代码为只有上公差
                                strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag);
                                strUp = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3);
                                strDown = "";
                                objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown);
                            }
                            else
                            {
                                intCodeStartFlag = strPreviewCodeTemp.IndexOf("<L>");
                                intCodeEndFlag = strPreviewCodeTemp.IndexOf("</L>");
                                if (intCodeStartFlag > -1)
                                {
                                    //代码为只有下公差
                                    strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag);
                                    strUp = "";
                                    strDown = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3);
                                    objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown);
                                }
                                else
                                {
                                    intCodeStartFlag = strPreviewCodeTemp.IndexOf("<√>");
                                    intCodeEndFlag = strPreviewCodeTemp.IndexOf("</√>");
                                    if (intCodeStartFlag > -1)
                                    {
                                        //代码为指定加工方法时
                                        strLeft = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3);

                                        strRight = strPreviewCodeTemp.Substring(intCodeEndFlag + 4);
                                        objBmp = oDraw.DrawRoughness(strLeft, strRight);
                                    }
                                    else
                                    {
                                        intCodeStartFlag = strPreviewCodeTemp.IndexOf("<R>");
                                        intCodeEndFlag = strPreviewCodeTemp.IndexOf("</R>");
                                        if (intCodeStartFlag > -1)
                                        {
                                            //代码为去除材料时
                                            strLeft = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3);

                                            strRight = "";
                                            objBmp = oDraw.DrawRoughness(strLeft, strRight);
                                        }
                                        else
                                        {
                                            intCodeStartFlag = strPreviewCodeTemp.IndexOf("<Q>");
                                            intCodeEndFlag = strPreviewCodeTemp.IndexOf("</Q>");
                                            if (intCodeStartFlag > -1)
                                            {
                                                //代码为不去除材料时
                                                strLeft = "";

                                                strRight = "";
                                                objBmp = oDraw.DrawRoughness(strLeft, strRight);
                                            }
                                            else
                                            {
                                                objBmp = null;
                                            }
                                        }
                                    }
                                }
                            }

                        }
                    }

                    //保存
                    if (objBmp != null)
                    {
                        //strFileDoc = strMapPath + @"\ImageTemp\";
                        //clsCon oCon = new clsCon();
                        String strImageTempPath;
                        //strImageTempPath = oCon.LoadConfigString("ImageTempPath");
                        strImageTempPath = webRootDir + ConfigurationManager.AppSettings["ImageTempPath"].ToString();

                        strFileDoc = strImageTempPath;
                        //if (Session["intMaxImageIndex"] == null)
                        //{
                        //    Session["intMaxImageIndex"] = 0;
                        //    intImageIndex = 1;
                        //}
                        //else
                        //{
                        //    intImageIndex = (int)Session["intMaxImageIndex"] + 1;
                        //}

                        strImageIndex = System.DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff");
                        strFileName = "ImageTemp-" + strImageIndex + ".png";
                        //strFileName = "ImageTemp-" + intImageIndex + ".png";
                        strFilePath = strFileDoc + strFileName;
                        //System.Threading.Thread.Sleep(1);
                        //System.IO.FileInfo oFile = new System.IO.FileInfo(strFilePath);
                        //while (oFile.Exists == true)
                        //{
                        //    intImageIndex += 1;
                        //    strFileName = "ImageTemp-" + intImageIndex + ".png";
                        //    strFilePath = strFileDoc + strFileName;
                        //    oFile = new System.IO.FileInfo(strFilePath);
                        //}

                        System.IO.FileInfo oFile = new System.IO.FileInfo(strFilePath);
                        while (oFile.Exists == true)
                        {
                            strImageIndex = strImageIndex + "1";
                            strFileName = "ImageTemp-" + strImageIndex + ".png";
                            strFilePath = strFileDoc + strFileName;
                            oFile = new System.IO.FileInfo(strFilePath);
                        }

                        objBmp.Save(strFilePath);
                        // Session["intMaxImageIndex"] = intImageIndex;
                        if (Session["dtImage"] == null)
                        {
                            dtImage = new DataTable();
                            dtImage.Columns.Add("FileName");
                            dtImage.Columns.Add("HtmlCode");
                            Session["dtImage"] = dtImage;
                        }
                        else
                        {
                            dtImage = (DataTable)Session["dtImage"];
                        }

                        DataRow dr;
                        dr = dtImage.NewRow();
                        dr[0] = strFileName;
                        dr[1] = "<Image>" + strPreviewCodeTemp + "</Image>";
                        dtImage.Rows.Add(dr);
                        Session["dtImage"] = dtImage;

                        //strFileDoc = oCon.LoadConfigString("ImageTempPath");
                        //strFileDoc = ConfigurationManager.AppSettings["ImageTempPath"].ToString();
                        strFileDoc = ResolveUrl(webRootUrl + ConfigurationManager.AppSettings["ImageTempPath"].ToString());

                         //strHtml = "<img src='../../../ImageTemp/" + strFileName + "'>";
                        strHtml = "http://" + common.GetIpAddress()+"/"+  strFileDoc + strFileName ;
                        strPreviewCodeDis += strHtml + "㊣";
                    }

                }
                if (intEndFlag + 8 < strPreviewCode.Length)
                {
                    strPreviewCode = strPreviewCode.Substring(intEndFlag + 8);
                    intStartFlag = strPreviewCode.IndexOf("<Image>");
                    intEndFlag = strPreviewCode.IndexOf("</Image>");
                }
                else
                {
                    intStartFlag = -1;
                }

            }
            intStartFlag = strPreviewCode.IndexOf("<Image>");
            if (strPreviewCode != "" && intStartFlag == -1)
            {
                strPreviewCodeDis += strPreviewCode;
            }
        }
        catch (Exception myError)
        {

        }

    }

       public  string MerageImagsAndStrs(string strValue,int fixedWidth=0)
        {

        List<object> para = new List<object>();
        System.Net.WebClient wc = new System.Net.WebClient();
        bool blCreatePic = false;
        if (strValue.Split('㊣').Length>0)
        {
            for ( int j= 0;j< strValue.Split('㊣').Length;j++)
            {
                if (!string.IsNullOrEmpty(strValue.Split('㊣')[j]))
                {

                    if (strValue.Split('㊣')[j].Contains("http"))
                    {
                        MemoryStream ms = new MemoryStream(wc.DownloadData(strValue.Split('㊣')[j]));
                        para.Add(Bitmap.FromStream(ms, true));
                        blCreatePic = true;
                    }
                    else
                    {
                        para.Add(strValue.Split('㊣')[j]);
                    }
                }

            }
        }
        string resultPath = "", filaName = "image-" + System.DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fffff") + ".png";
        if (blCreatePic == false || para.Count==1)
            return strValue.Replace("㊣","");

      
            int fontSize = 10;int fontPx = 7;//10号字体中文大约占14px，英文大约占7px

            int bmpWidth=0; int bmpHeight = 20;//画板宽度，高度
            int addHeight = 20;//增量高度
            int addCount = 0;//增加过高度的次数
            if (fixedWidth > 0)
            { 
                bmpWidth = fixedWidth;
            }

            int usedWidth = 0, usedHeight =0;

            for(int i=0;i<para.Count;i++) {
                var item = para[i];
                if (item.GetType() != typeof(string))
                {
                    System.Drawing.Image img = (System.Drawing.Image)item;

                    if (fixedWidth == 0)
                        bmpWidth += img.Width;
                    else {
                        usedHeight += addHeight;
                        usedWidth = 0;
                        //if (usedWidth + img.Width+1 <= fixedWidth)
                        //{
                        //    usedWidth += img.Width+1;
                        //}
                        //else {
                        //    usedHeight += addHeight;
                        //    usedWidth = 0;
                        //}
                    }
                }
                else if (item.GetType() == typeof(string)) {
                    int strPixelLength = System.Text.Encoding.Default.GetBytes(item.ToString()).Length * fontPx;//字符串所占用的像素长度 
                    if (fixedWidth == 0)
                        bmpWidth += strPixelLength;
                    else
                    {
                      
                        if (usedWidth + strPixelLength <= fixedWidth)
                        {
                            usedWidth += strPixelLength;
                        }
                        else
                        {
                            //
                            int addNum = (int)Math.Ceiling((usedWidth + strPixelLength) / float.Parse((fixedWidth).ToString()));
                            if (addCount == 0)
                                usedHeight += addHeight * (addNum - 1);
                            else
                                usedHeight += addHeight * (addNum);
                            usedWidth = 0;
                            addCount++;
                        }
                    }
                }
            }
            bmpHeight += usedHeight;

            //创建位图并获取绘图设备
            Bitmap objBmp = new Bitmap(bmpWidth, bmpHeight);                       
            System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(objBmp);
            graphics.Clear(Color.White);//背景默认为黑色，需要用白色替换一下
            //重置已使用宽度高度
            usedWidth = 0; usedHeight = 0; addCount = 0;

            foreach (var item in para)
            {
                if (item.GetType() != typeof(string))
                {
                    System.Drawing.Image img = (System.Drawing.Image)item;

                    if (fixedWidth == 0)
                    {
                        graphics.DrawImage(img, usedWidth, 1, img.Width, 20);
                        usedWidth += img.Width;
                    }
                    else
                    {
                       // usedHeight += addHeight; usedWidth = img.Width + 1;
                       // graphics.DrawImage(img, 0, usedHeight, img.Width, 20);
                        if (usedWidth + img.Width + 1 <= fixedWidth)
                        {
                            graphics.DrawImage(img, usedWidth, usedHeight, img.Width, 20);
                            usedWidth += img.Width + 1;
                        }
                        else
                        {
                            usedHeight += addHeight; usedWidth = img.Width + 1;
                            graphics.DrawImage(img, 0, usedHeight, img.Width, 20);
                        }
                    }

                }
                else if (item.GetType() == typeof(string))
                {
                    int strPixelLength = System.Text.Encoding.Default.GetBytes(item.ToString()).Length * fontPx;//字符串所占用的像素长度 System.Text.Encoding.UTF8.GetBytes(strPadleft).Length
                    
                    if (fixedWidth == 0)
                    {
                        graphics.DrawString(item.ToString(), new System.Drawing.Font("宋体", fontSize), new System.Drawing.SolidBrush(System.Drawing.Color.Black), new System.Drawing.Point(usedWidth, 0));
                        usedWidth += strPixelLength;
                    }
                    else
                    {
                        #region 自动换行
                        //RectangleF rec = new RectangleF(usedWidth, usedHeight, fixedWidth-usedWidth, bmpHeight);
                        RectangleF rec = new RectangleF(0, usedHeight, fixedWidth, bmpHeight); StringFormat sf = new StringFormat(); sf.LineAlignment = StringAlignment.Near;
                        string strPadleft = "";
                        while (System.Text.Encoding.Default.GetBytes(strPadleft).Length * fontSize <= usedWidth) {
                            strPadleft += " ";
                        }
                        strPadleft += "  ";

                        float test = graphics.MeasureString(strPadleft, new System.Drawing.Font("宋体", fontSize)).Width;

                        graphics.DrawString(strPadleft+item.ToString(), new System.Drawing.Font("宋体", fontSize), new System.Drawing.SolidBrush(System.Drawing.Color.Red), rec,sf);
                        #endregion

                        if (usedWidth + strPixelLength <= fixedWidth)
                        {
                            usedWidth += strPixelLength;
                        }
                        else
                        {
                            int addNum = (int)Math.Ceiling((usedWidth + strPixelLength) / float.Parse((fixedWidth).ToString()));
                            usedHeight += addHeight * (addNum);
                            usedWidth = 0;
                            addCount++;
                        }
                    }

                }
            }

           // System.Drawing.Image myImage = System.Drawing.Image.FromFile(imgPath);                     

            resultPath = HttpRuntime.AppDomainAppPath + ConfigurationManager.AppSettings["ImageTempPath"] + filaName;
            objBmp.Save(resultPath, System.Drawing.Imaging.ImageFormat.Png);
            graphics.Dispose();

        return "http://" + common.GetIpAddress() + "/" + ResolveUrl(webRootUrl + ConfigurationManager.AppSettings["ImageTempPath"].ToString()) + filaName;
        }
}